package com.eastfair.venueservice.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.eastfair.annotation.annotation.log.SysLog;
import com.eastfair.annotation.annotation.security.PreAuth;
import com.eastfair.boot.controller.SuperController;
import com.eastfair.boot.request.PageParams;
import com.eastfair.core.base.R;
import com.eastfair.core.context.ContextUtil;
import com.eastfair.core.exception.BizException;
import com.eastfair.echo.core.EchoService;
import com.eastfair.venueservice.dto.*;
import com.eastfair.venueservice.entity.ArrangementManage;
import com.eastfair.venueservice.service.ArrangementManageService;
import com.eastfair.venueservice.vo.ArrangementManageVO;
import com.eastfair.venueservice.vo.ExhibitionManageVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;


/**
 * <p>
 * 前端控制器
 * 布撤展现场管理
 * </p>
 *
 * @author lyw
 * @date 2022-08-11
 */
@Slf4j
@Validated
@RestController
@RequestMapping("/arrangementManage")
@Api(value = "ArrangementManage", tags = "布撤展现场管理")
@PreAuth(replace = "venueservice:arrangementManage:")
public class ArrangementManageController extends SuperController<ArrangementManageService, Long, ArrangementManage, ArrangementManagePageQuery, ArrangementManageSaveDTO, ArrangementManageUpdateDTO> {

    @Resource
    private EchoService echoService;

    @Resource
    private ArrangementManageService arrangementManageService;

    @Override
    public void handlerResult(IPage<ArrangementManage> page) {
        // 想让返回值实现自动回显，请将此行代码打开
        // echoService.action(page);
    }

    @Override
    public R<Boolean> handlerImport(List<Map<String, String>> list) {
        List<ArrangementManage> arrangementManageList = list
                .stream()
                .map((map) -> ArrangementManage.builder().build())
                .collect(Collectors.toList());
        return R.success(baseService.saveBatch(arrangementManageList));
    }

    @Override
    public void handlerQueryParams(PageParams<ArrangementManagePageQuery> params) {
        ArrangementManagePageQuery pageQuery = params.getModel();
        //分页处理
        if (ContextUtil.getProjectId() != null) {
            pageQuery.setProjectId(ContextUtil.getProjectId());
        }
    }

    /**
     * 通过服务商查询进行中的展会.
     *
     * @param exhibitionManageQuery 通过服务商查询进行中的展会
     * @return R
     */
    @PostMapping("/queryExhibitionManageByProvider")
    R<ExhibitionManageVO> queryExhibitionManageByProvider(@RequestBody ExhibitionManageQuery exhibitionManageQuery) {
        log.info("queryExhibitionManageByProvider - 通过服务商查询进行中的展会, exhibitionManageQuery={}", exhibitionManageQuery);
        ExhibitionManageVO exhibitionManageVO = arrangementManageService.queryExhibitionManageByProvider(exhibitionManageQuery);
        return R.success(exhibitionManageVO);
    }

    /**
     * 主场巡检查询布撤展管理记录
     *
     * @param params the params
     * @return r
     */
    @ApiOperation(value = "主场巡检查询布撤展管理记录")
    @PostMapping(value = "/query/for/homeCheck")
    public R<Page<ArrangementManageVO>> queryForHomeCheck(@RequestBody PageParams<ArrangementManageDTO> params) {
        log.info("queryForHomeCheck - 主场巡检查询布撤展管理记录, params={}", params);
        try {
            Page<ArrangementManageVO> page = arrangementManageService.queryForHomeCheck(params);
            return R.success(page);
        } catch (BizException biz) {
            return R.fail(biz);
        }
    }

    /**
     * 主场查询布撤展管理记录.
     *
     * @param params the params
     * @return the r
     */
    @PostMapping(value = "/query/for/home")
    public R<Page<ArrangementManageVO>> queryForHome(@RequestBody PageParams<ArrangementManageDTO> params) {
        log.info("queryForHome - 主场查询布撤展管理记录, params={}", params);
        try {
            Page<ArrangementManageVO> page = arrangementManageService.queryForHome(params);
            return R.success(page);
        } catch (BizException biz) {
            return R.fail(biz);
        }
    }

    /**
     * 查询布撤展管理详情
     *
     * @param arrangementManageDTO the arrangement manage dto
     * @return arrangement manage
     */
    @ApiOperation(value = "查询布撤展管理详情")
    @PostMapping(value = "/queryArrangementManageInfo")
    public R<ArrangementManageVO> getArrangementManage(@RequestBody ArrangementManageDTO arrangementManageDTO) {
        log.info("getArrangementManage - 查询布撤展管理详情, arrangementManageDTO={}", arrangementManageDTO);
        try {
            ArrangementManageVO arrangementManageVO = arrangementManageService.getArrangementManage(arrangementManageDTO);
            return R.success(arrangementManageVO);
        } catch (BizException biz) {
            return R.fail(biz);
        }
    }

    /**
     * 主场巡检保存布撤展管理
     *
     * @param arrangementManageDTO the arrangement manage dto
     * @return r
     */
    @ApiOperation(value = "主场巡检保存布撤展管理")
    @SysLog("主场巡检保存布撤展管理")
    @PostMapping(value = "/submit/for/homeCheck")
    public R<Boolean> submitForHomeCheck(@RequestBody ArrangementManageDTO arrangementManageDTO) {
        log.info("submitForHome - 主场巡检保存布撤展管理, arrangementManageDTO={}", arrangementManageDTO);
        try {
            return R.success(arrangementManageService.submitForHomeCheck(arrangementManageDTO));
        } catch (BizException biz) {
            return R.fail(biz);
        }
    }

    /**
     * 修改布撤展管理
     *
     * @param arrangementManageDTO the arrangement manage dto
     * @return r
     */
    @SysLog("修改布撤展管理记录")
    @ApiOperation(value = "修改布撤展管理")
    @PostMapping(value = "/updateArrangementManageInfo")
    public R<Boolean> updateArrangementManage(@RequestBody ArrangementManageDTO arrangementManageDTO) {
        log.info("updateArrangementManage - 修改布撤展管理, arrangementManageDTO={}", arrangementManageDTO);
        try {
            return R.success(arrangementManageService.updateArrangementManage(arrangementManageDTO));
        } catch (BizException biz) {
            return R.fail(biz);
        }
    }

    /**
     * 删除布撤展
     *
     * @param id the id
     * @return r
     */
    @ApiOperation(value = "删除布撤展")
    @SysLog("删除布撤展管理记录")
    @GetMapping(value = "/delArrangementManageInfo/{id}")
    public R<Boolean> delArrangementManage(@PathVariable Long id) {
        log.info("delArrangementManage - 删除布撤展, id={}", id);
        try {
            return R.success(arrangementManageService.delArrangementManage(id));
        } catch (BizException biz) {
            return R.fail(biz);
        }
    }

}
